Skip to content

Apply use_reentrant removal to all TRL trainer configs#4321

Merged
danielhanchen merged 1 commit into
mainfrom
fix-gradient-checkpointing-transformers-5.2
Mar 16, 2026
Merged

Apply use_reentrant removal to all TRL trainer configs#4321
danielhanchen merged 1 commit into
mainfrom
fix-gradient-checkpointing-transformers-5.2

Conversation

@danielhanchen
Copy link
Copy Markdown
Member

Summary

The existing fix in rl.py that removes use_reentrant=False from gradient_checkpointing_kwargs (added for TRL 0.27.0+) was gated behind RLConfig_name == "GRPOConfig". This meant only GRPOConfig was protected from the VRAM regression, while SFTConfig, DPOConfig, KTOConfig, CPOConfig, ORPOConfig and all other trainer configs were still affected.

Change

Remove the GRPOConfig guard so the use_reentrant removal applies to all compiled trainer configs when TRL >= 0.27.0.

# Before:
if trl_version >= Version("0.27.0") and RLConfig_name == "GRPOConfig":
# After:
if trl_version >= Version("0.27.0"):

Why this is safe

  • The version guard trl_version >= Version("0.27.0") is preserved, so TRL < 0.27.0 is unaffected
  • The removal code only deletes use_reentrant from gradient_checkpointing_kwargs if it exists, using getattr and in checks
  • This is defense-in-depth alongside the primary fix in unsloth-zoo (Fix VRAM regression with transformers 5.2+ gradient checkpointing unsloth-zoo#549) which forces use_reentrant=True in unsloth_checkpoint() itself

Compatibility

  • TRL 0.22.2 through 0.26.x: unaffected (version guard prevents execution)
  • TRL 0.27.0+: now applies to all trainer configs, not just GRPO
  • Transformers 4.57.x through 5.3+: compatible (the removal is a no-op if use_reentrant is not set)

The existing fix that removes use_reentrant=False from
gradient_checkpointing_kwargs was gated behind RLConfig_name ==
"GRPOConfig", so only GRPOConfig was protected. SFTConfig, DPOConfig,
KTOConfig, CPOConfig, ORPOConfig etc. were all still affected.

Remove the GRPOConfig guard so the fix applies to all compiled trainer
configs when TRL >= 0.27.0.

This is defense-in-depth alongside the unsloth_zoo fix that forces
use_reentrant=True in unsloth_checkpoint() itself.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request expands a critical VRAM regression fix to cover all TRL trainer configurations. Previously, the fix was inadvertently limited to only GRPOConfig, leaving other trainers vulnerable to increased VRAM usage with TRL versions 0.27.0 and above. By removing a specific condition, this change ensures consistent application of the necessary use_reentrant removal across the entire TRL ecosystem, improving stability and resource efficiency for all users.

Highlights

  • Broadened use_reentrant removal: The existing fix that removes use_reentrant=False from gradient_checkpointing_kwargs now applies to all TRL trainer configurations (SFTConfig, DPOConfig, KTOConfig, CPOConfig, ORPOConfig, etc.) when TRL version is 0.27.0 or higher, instead of being limited only to GRPOConfig.
Changelog
  • unsloth/models/rl.py
    • Removed the RLConfig_name == "GRPOConfig" condition from the if statement that governs the use_reentrant removal logic.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly expands a fix for a VRAM regression to all TRL trainer configurations. Previously, the patch to remove use_reentrant=False from gradient_checkpointing_kwargs in TRL versions 0.27.0+ was limited to GRPOConfig. By removing this restriction, the fix now applies to all trainer configs, ensuring consistent behavior and preventing the regression in a wider range of scenarios. The change is safe, as it is guarded by the TRL version check and the patch itself defensively checks for the existence of the attribute and key before attempting deletion.

@danielhanchen danielhanchen merged commit 356538d into main Mar 16, 2026
5 checks passed
@danielhanchen danielhanchen deleted the fix-gradient-checkpointing-transformers-5.2 branch March 16, 2026 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant